package com.pureut.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.system.api.StorageFegin;
import com.pureut.system.domain.MaterialCoding;
import com.pureut.system.domain.dto.MaterialCodingMqDto;
import com.pureut.system.domain.dto.SysTheDropDto;
import com.pureut.system.domain.dto.WmsStockInfoDto;
import com.pureut.system.mapper.MaterialCodingMapper;
import com.pureut.system.service.IMaterialCodingService;
import com.pureut.system.util.MaterialMqUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 类别实现类
 * @Param
 * @Date 22-11-24
 * @Author hl
 **/
@Service
public class MaterialCodingServiceImpl implements IMaterialCodingService {
    @Resource
    private MaterialCodingMapper materialCodingMapper;

    @Resource
    private StorageFegin storageFegin;

    @Override
    public int codingAdd(MaterialCoding materialCoding) throws Exception {
        int in = materialCodingMapper.codingCheckUniqueness(materialCoding.getCodingCode());
        if (in > 0) {
            throw new SQLException("类别编码不能重复");
        }
        //mq发送消息
        MaterialCodingMqDto materialCodingMqDto = new MaterialCodingMqDto();
        materialCodingMqDto.setCodingCode(materialCoding.getCodingCode());
        materialCodingMqDto.setCodingOf(materialCoding.getCodingOf());
        materialCodingMqDto.setCodingRules(materialCoding.getCodingRules());
        materialCodingMqDto.setCodingType(String.valueOf(materialCoding.getCodingType()));

        MaterialMqUtil.materialCodingMqSend(materialCodingMqDto);
        return materialCodingMapper.codingAdd(materialCoding);
    }

    @Override
    public int updateCodingEdit(MaterialCoding materialCoding) throws Exception {
        //mq发送消息
        MaterialCodingMqDto materialCodingMqDto = new MaterialCodingMqDto();
        materialCodingMqDto.setCodingId(materialCoding.getCodingId());
        materialCodingMqDto.setCodingCode(materialCoding.getCodingCode());
        materialCodingMqDto.setCodingOf(materialCoding.getCodingOf());
        materialCodingMqDto.setCodingRules(materialCoding.getCodingRules());
        materialCodingMqDto.setCodingType(String.valueOf(materialCoding.getCodingType()));
        MaterialMqUtil.materialCodingMqSend(materialCodingMqDto);
        return materialCodingMapper.updateCodingEdit(materialCoding);
    }

    @Override
    public int deleteCodingById(long codingId) throws Exception {
        //mq发送删除类别
        MaterialCodingMqDto materialCodingMqDto = new MaterialCodingMqDto();
        MaterialCoding materialCoding = materialCodingMapper.selectListCoding(codingId);
        materialCodingMqDto.setCodingCode(materialCoding.getCodingCode());
        materialCodingMqDto.setMark(2);
        MaterialMqUtil.materialCodingMqSendDelete(materialCodingMqDto);
        return materialCodingMapper.deleteCodingById(codingId);
    }


    @Override
    public List<SysTheDropDto> selectCodingListThe(int condingId) {
        return materialCodingMapper.selectCodingListThe(condingId);
    }

    @Override
    public MaterialCoding selectListCoding(long codingId) {
        return materialCodingMapper.selectListCoding(codingId);
    }

    /**
     * 根据原料编码查询类型
     *
     * @param codingId
     * @return
     */
    @Override
    public List<MaterialCoding> selectCodingId(String codingId) {

        List<MaterialCoding> list = new ArrayList<>();

        if (codingId!=null){
            List<MaterialCoding> materialCodings = materialCodingMapper.selectCodingId(codingId);
            list.addAll(materialCodings);
        }

        return list;
    }

    /**
     * 根据原料编码查询类型
     *
     * @param codingId
     * @return
     */
    @Override
    public MaterialCoding selectCodingRulesById(String codingId) {
        MaterialCoding materialCoding = materialCodingMapper.selectCodingRulesById(codingId);
        return materialCoding;
    }

    /**
     * 查询物料类别
     * @param
     * @return
     */
    @Override
    public List<MaterialCoding> selectMaterialCodingList() {
        return materialCodingMapper.selectMaterialCodingList();
    }


}
